home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / S / SERIAL.PAS.sit / SERIAL.PAS
Encoding:
Pascal/Delphi Source File  |  1991-01-20  |  5.0 KB  |  215 lines  |  [TEXT/QED1]

  1. { Serial Demo program adapted from SerialDemo.c by Mark Y. Geschelin.}
  2. { This program uses the modem port to send and recieve characters.}
  3. { It functions as a very simple terminal emulator.  This is meant to be an}
  4. { example of the use of the Serial Manager, not an example of how to code}
  5. { a terminal emulator!!}
  6. { Compile this program with Runtime.lib, Interface.lib, and Serial.p}
  7. { Pascal port by Phil Shapiro and Mark Y. Geschelin, ) 1990 Symantec Corp.}
  8. {  Revised:}
  9. {        7-3-90   Myg    added handshaking and made buffer larger as well as some special processing because}
  10. {                                 i found the default behavior really annoying during testing}
  11.  
  12. program SerialDemo (input, output);
  13.     uses
  14.         Serial;
  15.     const
  16.         EscapeChar = $1b;
  17.         LinefeedChar = $0a;
  18.         backspacechar = $08;
  19.         deletechar = $7f;
  20.         Echo = false;
  21.         recieve_raw = false;  { set this to true to see the raw data as sent - yuk}
  22.         transmit_raw = false; { set this to true to send the data exactly as typed -yuk}
  23.         BufferLen = 1024;
  24.         xonchar = char($11);
  25.         xoffchar = char($13);
  26.         hatchar = $5E;
  27.     type
  28.         BufferType = packed array[1..BufferLen] of signedbyte;
  29.         BufferPtr = ^BufferType;
  30.     var
  31.         inBuf: BufferPtr;                            {our buffer}
  32.         SerialManagerBuffer: BufferPtr;  {the buffer for the serial manager}
  33.         flags: sershk;                                {data structure to set up handshaking}
  34.  
  35.     function AvailChar: char;                {pole for data from keyboard}
  36.         var
  37.             c: char;
  38.             event: EventRecord;
  39.             foo: boolean;
  40.         function interpret_output (xy: signedbyte): char;
  41.         begin
  42.             if transmit_raw then
  43.                 interpret_output := char(xy)
  44.             else
  45.                 case xy of
  46.                     backspacechar: 
  47.                         interpret_output := char(deletechar);
  48.                     linefeedchar: 
  49.                         ;
  50.                     otherwise
  51.                         interpret_output := char(xy);
  52.                 end;
  53.         end;
  54.     begin
  55.         c := char(0);
  56.         if getnextevent(everyevent, event) then
  57.             if (event.what = keyDown) or (event.what = autoKey) then
  58.                 begin
  59.                     c := interpret_output(BAND(event.message, charCodeMask));
  60.                 end;
  61.         AvailChar := c;
  62.     end;
  63.  
  64.     procedure CleanUp;
  65.     begin
  66.         RAMSDClose(sPortA);
  67.         if inbuf <> nil then
  68.             DisposPtr(Ptr(inbuf));
  69.     end;
  70.  
  71.     procedure DisplayBuff (count: longint);
  72.         var
  73.             i: longint;
  74.             hatflag: boolean;
  75.         procedure interpret (x: signedbyte);
  76.         begin
  77.             if hatflag then
  78.                 hatflag := false
  79.             else
  80.                 begin
  81.                     if recieve_raw then
  82.                         write(char(x))
  83.                     else
  84.                         case x of
  85.                             linefeedchar: 
  86.                                 ;
  87.                             backspacechar: 
  88.                                 ;
  89.                             hatchar: 
  90.                                 hatflag := true;
  91.                             otherwise
  92.                                 write(char(x));
  93.                         end;
  94.                 end;
  95.         end;
  96.     begin
  97.         hatflag := false;
  98.         for i := 1 to count do
  99.             interpret(inbuf^[i]);
  100.     end;
  101.  
  102.     procedure GetSerialChars (count: longint);
  103.         var
  104.             err: OSErr;
  105.     begin
  106.         err := FSRead(AinRefNum, count, Ptr(inbuf));
  107.     end;
  108.  
  109.     function SerialCharsAvail: integer;
  110.         var
  111.             count: longint;
  112.             err: OSErr;
  113.     begin
  114.         err := SerGetBuf(AinRefNum, count);
  115.         SerialCharsAvail := count
  116.     end;
  117.  
  118.     procedure SerialWrite (ch: char);
  119.         var
  120.             err: OSErr;
  121.             num: longint;
  122.             cha: signedbyte;
  123.     begin
  124.         num := 1;
  125.         cha := signedbyte(ch);
  126.         err := FSWrite(AoutRefNum, num, Ptr(ord4(@cha)))
  127.     end;
  128.  
  129.     function SerialInit: OSErr;
  130.         var
  131.             err: OSErr;
  132.             flags: sershk;
  133.     begin
  134.         with flags do
  135.             begin
  136.                 fxon := byte(TRUE);
  137.                 finx := byte(TRUE);
  138.                 xon := xonchar;
  139.                 xoff := xoffchar;
  140.             end;
  141.         new(serialmanagerbuffer);
  142.         new(inbuf);
  143.         err := RAMSDOpen(sPortA);
  144.         if err = noErr then
  145.             begin
  146.                 err := SerReset(AinRefNum, baud:2400 + data8 + stop10 + noParity);
  147. {make a large input buffer}
  148.                 err := err + sersetbuf(ainrefnum, ptr(serialmanagerbuffer), sizeof(buffertype));
  149. {and even with the large input buffer set it up so it will send an xoff when the buffer is full }
  150. {see the flags structure}
  151.                 err := err + serHshake(ainrefnum, flags);
  152.                 if err = noErr then
  153.                     begin
  154.                         err := SerReset(AoutRefNum, baud2400 + data8 + stop10 + noParity);
  155.                     end
  156.             end;
  157.         if err <> noErr then
  158.             RAMSDClose(sPortA);
  159.         SerialInit := err;
  160.     end;
  161.  
  162.     procedure Introduction;
  163.         var
  164.             r: Rect;
  165.     begin
  166.         SetRect(r, 5, 40, 475, 310);
  167.         SetTextRect(r);
  168.         ShowText;
  169.         writeln('This program reads and writes to the modem port at 2400 baud.');
  170.         writeln('It uses 8 data bits, 1 stop bits, and no parity.');
  171.         writeln('Press the <ESC> key to exit');
  172.     end;
  173.  
  174.     procedure Main;
  175.         var
  176.             err: OSErr;
  177.             count: integer;
  178.             ch: char;
  179.     begin
  180.         err := SerialInit;
  181.         if err = noErr then
  182.             begin
  183.                 ch := AvailChar;
  184.                 while ord(ch) <> EscapeChar do
  185.                     begin
  186.                         if ord(ch) <> 0 then
  187.                             begin
  188.                                 SerialWrite(ch);
  189.                                 if echo then
  190.                                     write(ch)
  191.                             end;
  192.                         count := SerialCharsAvail;
  193. {see what happens if you comment out the serhshake and leave this code in}
  194. {    if count > (bufferlen div 2) then}
  195. {    begin}
  196. {    writeln;}
  197. {    writeln('were are getting full warning count =', count, ' if this = buflen-1 chances are we overran');}
  198. {    end;}
  199.                         if count <> 0 then
  200.                             begin
  201.                                 GetSerialChars(count);
  202.                                 DisplayBuff(count)
  203.                             end;
  204.                         ch := AvailChar;
  205.                     end;     {while}
  206.             end
  207.         else
  208.             writeln('The serial initializations have failed, id = ', err);
  209.     end;
  210.  
  211. begin
  212.     Introduction;
  213.     Main;
  214.     CleanUp
  215. end